package com.neusoft.neuiotms.micro.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.neusoft.neuiotms.micro.auth.AuthMicroService;
import com.neusoft.neuiotms.micro.cmdb.CmdbMicroService;
import com.neusoft.neuiotms.micro.databus.DatabusMicroService;
import com.neusoft.neuiotms.micro.notice.NoticeMicroService;

import feign.Contract;
import feign.codec.Decoder;
import feign.codec.Encoder;

@Configuration
@Import(FeignClientsConfiguration.class)
public class FeignClientConfiguration {

    @Value("${neuiotms.micro.service.auth.name:auth}")
    private String authServiceName;

    @Value("${neuiotms.micro.service.notice.name:notice}")
    private String noticeServiceName;

    @Value("${neuiotms.micro.service.databus.name:core}")
    private String databusServiceName;

    @Value("${neuiotms.micro.service.cmdb.name:cmdb}")
    private String cmdbServiceName;

    private final FeignClientBuilder feignClientBuilder;

    public FeignClientConfiguration(@Autowired final ApplicationContext applicationContext) {
        this.feignClientBuilder = new FeignClientBuilder(applicationContext);
    }

    @Bean
    public AuthMicroService authMicroService(final Contract contract, final Decoder decoder, final Encoder encoder) {
        return this.feignClientBuilder.forType(AuthMicroService.class, this.authServiceName).build();
    }

    @Bean
    public NoticeMicroService noticeMicroService(final Contract contract, final Decoder decoder,
            final Encoder encoder) {
        return this.feignClientBuilder.forType(NoticeMicroService.class, this.noticeServiceName).build();
    }

    @Bean
    public DatabusMicroService databusMicroService(final Contract contract, final Decoder decoder,
            final Encoder encoder) {
        return this.feignClientBuilder.forType(DatabusMicroService.class, this.databusServiceName).build();
    }

    @Bean
    public CmdbMicroService cmdbMicroService(final Contract contract, final Decoder decoder,
            final Encoder encoder) {
        return this.feignClientBuilder.forType(CmdbMicroService.class, this.cmdbServiceName).build();
    }
}
